package net.gz01.wallet.web.service;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.imageio.ImageIO;

public class ExcelPNGUtil {
    public static String graphicsGeneration(List<List<List<String>>> allValue,
                                            List<String> titles,List<String[]> headers ,
                                            String receiver,int totalcol) throws Exception {
        int rows = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                rows += (2+typeV.size());
            }
        }

        final int Growheight = 40;

        // 实际数据行数+标题+备注
        int totalrow = 1+rows;
        int imageWidth = 1300;
        int imageHeight = totalrow * Growheight + 20;
        int rowheight = Growheight;
        int startHeight = 10;
        int startWidth = 10;
        int colwidth = ((imageWidth - 20) / totalcol);

        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();

        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, imageWidth, imageHeight);
        //画背景
       // graphics.setColor(new Color(0, 112, 192));
        graphics.setColor(Color.WHITE);
        int startH = 1;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                graphics.fillRect(startWidth+1, startHeight+startH*rowheight+1, imageWidth - startWidth-5-1,rowheight-1);
                startH+=2+typeV.size();
            }
        }

        graphics.setColor(new Color(220, 240, 240));
        // 画横线

        for (int j = 0; j < totalrow - 1; j++) {
            graphics.setColor(Color.PINK);
            graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, imageWidth - 5,
                    startHeight + (j + 1) * rowheight);
        }

        // 画竖线
        graphics.setColor(Color.WHITE);
        startH = 1;
        int rightLine = 0 ;
        for (List<List<String>> typeV : allValue) {

            if (typeV != null && typeV.size() > 0) {
                for (int k = 0; k < totalcol+1; k++) {
                    rightLine = getRightMargin(k,startWidth, colwidth,imageWidth);
                    graphics.drawLine(rightLine, startHeight + startH*rowheight, rightLine,
                            startHeight + (typeV.size()+1+startH)*rowheight);
                }
                startH+=2+typeV.size();
            }
        }

        // 设置字体
        graphics.setColor(Color.BLACK);
        Font font = new Font("华文楷体", Font.BOLD, 20);
        graphics.setFont(font);

        // 写标题
        startH = 1;
        int i = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                graphics.drawString(titles.get(i), imageWidth / 3 + startWidth+30, startHeight + startH*rowheight - 10);
                startH+=2+typeV.size();
            }
            i++;
        }


        // 写入表头
        graphics.setColor(Color.BLACK);
        font = new Font("华文楷体", Font.BOLD, 20);
        graphics.setFont(font);
        startH = 2;
        i = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {

                String[] headCells = headers.get(i);
                for (int m = 0; m < headCells.length; m++) {
                    rightLine = getRightMargin(m,startWidth, colwidth,imageWidth)+5;
                    graphics.drawString(headCells[m].toString(), rightLine,
                            startHeight + rowheight * startH - 10);
                }
                startH+=2+typeV.size();
            }
            i++;
        }


        // 写入内容
        graphics.setColor(Color.black);
        font = new Font("华文楷体", Font.PLAIN, 20);
        graphics.setFont(font);
        startH = 3;
        i = 0;
        for (List<List<String>> typeV : allValue) {
            if (typeV != null && typeV.size() > 0) {
                for (int n = 0; n < typeV.size(); n++) {
                    List<String> arr = typeV.get(n);
                    for (int l = 0; l < arr.size(); l++) {
                        rightLine = getRightMargin(l,startWidth, colwidth,imageWidth)+5;
                        graphics.drawString(arr.get(l).toString(), rightLine,
                                startHeight + rowheight * (n + startH) - 10);
                    }
                }
                startH+=2+typeV.size();
            }
            i++;
        }

        String path =  receiver;//"1.jpg";
        ImageIO.write(image, "jpg", new File(path));
        return path;
    }

    /**
     * 获取竖线和文字的水平位置
     * @param k
     * @param startWidth
     * @param colwidth
     * @param imageWidth
     * @return
     */
    private static int getRightMargin(int k, int startWidth, int colwidth, int imageWidth) {
        int rightLine = 0;
        if (k == 0) {
            rightLine = startWidth;
        } else if (k == 1) {
            rightLine = startWidth + colwidth / 2;
        } else if (k == 2) {
            rightLine = startWidth + 3 * colwidth / 2;
        } else if (k == 3) {
            rightLine = startWidth + 7 * colwidth / 2;
        } else if (k == 4) {
            rightLine = imageWidth - 5;
        }
        return rightLine;
    }
}


class ImageTest {

    public static  void initChartData() {
        List<List<List<String>>> allValue = new ArrayList<>();
        List<String> content1 = Arrays.asList(new String[]{"刘丹丹", "25", "163cm", "未婚"});
        List<String> content2 = Arrays.asList(new String[]{"刘丹丹", "25", "163cm", "未婚"});
        List<String> content3 = Arrays.asList(new String[]{"刘丹丹", "宿迁", "本科", "未婚"});
        List<List<String>> contentArray1 = new ArrayList<>();
        contentArray1.add(content1);
        contentArray1.add(content2);
        List<List<String>> contentArray2 = new ArrayList<>();
        contentArray2.add(content3);
        allValue.add(contentArray1);
        allValue.add(contentArray2);

        List<String[]> headTitles = new ArrayList<>();
        String[] h1 = new String[]{"名字", "年龄", "身高", "婚姻"};
        String[] h2 = new String[]{"名字", "籍贯", "学历", "婚姻"};
        headTitles.add(h1);
        headTitles.add(h2);

        List<String> titles = new ArrayList<>();
        titles.add("友利钱包充值回单");
        titles.add("友利钱包提款回单");
        try {
            ExcelPNGUtil.graphicsGeneration(allValue, titles, headTitles, "", 4);
        }catch (Exception e){e.printStackTrace();}
    }

    public static  void ylinitChartData() {
        List<List<List<String>>> allValue = new ArrayList<>();
        List<String> content1 = Arrays.asList(new String[]{"回执日期:", "2019-03-01"});
        //List<String> content2 = Arrays.asList(new String[]{"刘丹丹", "25", "163cm", "未婚"});
        List<String> content3 = Arrays.asList(new String[]{"刘丹丹", "6226357889988777987", "中国工商银行我的一个人的" +
               "字路上的幸运的516路支行"});
        List<List<String>> contentArray1 = new ArrayList<>();
        contentArray1.add(content1);
        //contentArray1.add(content2);
        List<List<String>> contentArray2 = new ArrayList<>();
        contentArray2.add(content3);
        allValue.add(contentArray1);
        allValue.add(contentArray2);
        List<List<String>> contentArray3 = new ArrayList<>();

        List<String> contentC3 = Arrays.asList(new String[]{"王文武", "6226357889988777997", "中国工商银行我的一个人的" +
                "字路上我们去往生活的的幸运的516路支行"});
        contentArray3.add(contentC3);
        allValue.add(contentArray3);


        List<String[]> headTitles = new ArrayList<>();
        String[] h1 = new String[]{"回执单号:"+"      123456789012345678901234567890"}; //"回执日期:" + "2019-03-01"
        String[] h2 = new String[]{"户名", "账户", "开户行"};
        String[] h3 = new String[]{"户名", "账户", "开户行"};
        String[] h4 = new String[]{"金额:" + "10089.99"};
        headTitles.add(h1);
        headTitles.add(h2);
        headTitles.add(h3);

        List<String> titles = new ArrayList<>();
        titles.add("深圳友利金融有限公司 充值回单");
        titles.add("---付款信息---");
        titles.add("---收款信息---");

        try {
            ExcelPNGUtil.graphicsGeneration(allValue, titles, headTitles, "D:\\soeasy\\sss.jpg", 3);
        }catch (Exception e){e.printStackTrace();}
    }


    public static void main(String[] args) {
        ylinitChartData();

    }
}
